﻿using System;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using Ipmce.RemoteVoting.Backend.VotingService.Contract;
using Ipmce.RemoteVoting.Common.Tools.Wcf;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Ipmce.RemoteVoting.Backend.VotingService.UnitTests
{
	[TestClass]
	public class ServiceTest
	{
		static ServiceTest()
		{
			ServicePointManager.ServerCertificateValidationCallback =
				(sender, certificate, chain, policy) => CheckCertificate(sender, certificate, chain, policy);
		}

		[TestMethod]
		public void TestPassportVote()
		{
			using (var client = new ServiceInterfaceProxy<IPassportVoting>("VotingServiceEndPoint"))
			{
				if (client.ClientCredentials != null)
				{
					client.ClientCredentials.UserName.UserName = "test";
					client.ClientCredentials.UserName.Password = "the best";
					//client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode =
					//    X509CertificateValidationMode.Custom;
					//client.ClientCredentials.ServiceCertificate.Authentication.CustomCertificateValidator =
					//    new CertificateIssuerCnValidator("Monica");
				}
				client.GetInterface().Vote(new byte[0], "");
			}
		}

		[TestMethod]
		public void TestGetCandidateList()
		{
			using (var proxy = new ServiceInterfaceProxy<ICandidateListProvider>("CandidateServiceEndPoint"))
			{
				var result = proxy.GetInterface().GetCandidateList();
				Assert.IsNotNull(result);
				Assert.IsTrue(result.Any());
			}
		}

		private static bool CheckCertificate(Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
		{
			return true;
		}
	}
}
